﻿using BellAssistant.DSS;
using BellAssistant.Model.Tables;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BellAssistant.DataManager
{
    public partial class ExpressTypeManage : FrmBase
    {
        public ExpressTypeManage()
        {
            InitializeComponent();
            btnAdd.Click += btnAdd_Click;
            btnSearch.Click += btnSearch_Click;
            this.Load += ExpressTypeManage_Load;
        }

        private void ExpressTypeManage_Load(object sender, EventArgs e)
        {
            btnSearch_Click(sender, e);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            List<ExpressType> list = CreateDataSourceService.IDataSourceService.GetAllExpressType();
            if (string.IsNullOrEmpty(txtExpressTypeName.Text)&&string.IsNullOrEmpty(txtExpressPlace.Text))
            {
                gridControl1.DataSource = list;
            }
            else
            {
                gridControl1.DataSource = null;
                gridControl1.DataSource = list.FindAll(i=>i.ExpressTypeName.Contains(txtExpressTypeName.Text)||i.ExpressFromAddress.Contains(txtExpressPlace.Text));
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ExpressType expressType = new ExpressType();
            string str = IsINputStempyValue();
            if (str == "")
            {
                expressType.ExpressTypeName = txtExpressTypeName.Text;
                expressType.ExpressFromAddress = txtExpressPlace.Text;
                expressType.ExpressAtypePrice = Convert.ToInt32(txtAprice.Text);
                expressType.ExpressBtypePrice = Convert.ToInt32(txtBprice.Text);
                expressType.ExpressCtypePrice = Convert.ToInt32(txtCprice.Text);
                expressType.ExpressDtypePrice = Convert.ToInt32(txtDprice.Text);
                expressType.ExpressEtypePrice = Convert.ToInt32(txtEprice.Text);
                string result = CreateDataSourceService.IDataSourceService.SaveExpressType(expressType);
                MessageBox.Show(result, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            btnSearch_Click(sender, e);
            ClearAll();
        }

        private string IsINputStempyValue()
        {
            string result = "";
            if (string.IsNullOrEmpty(txtExpressTypeName.Text) || string.IsNullOrEmpty(txtExpressPlace.Text) || string.IsNullOrEmpty(txtAprice.Text) || string.IsNullOrEmpty(txtBprice.Text) || string.IsNullOrEmpty(txtCprice.Text) || string.IsNullOrEmpty(txtDprice.Text) || string.IsNullOrEmpty(txtEprice.Text))
            {
                result = "信息不能为空！";
            }
            return result;
        }

        private void ClearAll()
        {
            this.txtAprice.Text = "";
            this.txtBprice.Text = "";
            this.txtDprice.Text = "";
            this.txtCprice.Text = "";
            this.txtEprice.Text = "";
            this.txtExpressPlace.Text = "";
            this.txtExpressTypeName.Text = "";
        }
    }
}
